package com.xwintop.xJavaFxTool.controller.littleTools;

import com.xwintop.xJavaFxTool.services.littleTools.FileAllService;
import com.xwintop.xJavaFxTool.view.littleTools.FileAllView;
import com.xwintop.xcore.util.javafx.FileChooserUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import java.io.File;
import javafx.application.Platform;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

@Getter
@Setter
@Slf4j
public class FileAllController extends FileAllView {

    private FileAllService fileAllService = new FileAllService(this);
    private String[] fileTypeChoiceBoxStrings = new String[]{"剪切", "复制"};

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initView();
        initEvent();
        initService();
    }

    private void initView() {
        moveTypeChoiceBox.getItems().addAll(fileTypeChoiceBoxStrings);
        moveTypeChoiceBox.getSelectionModel().select(0);
    }

    private void initEvent() {
    }

    private void initService() {
    }

    @FXML
    private void selectFolderAction(ActionEvent event) {
        File file = FileChooserUtil.chooseDirectory();
        if (file != null) {
            selectFileTextField.setText(file.getPath());
        }
    }

    @FXML
    private void mergeAction(ActionEvent event) {
        TooltipUtil.showToast("正在进行文件操作请稍后...");
        new Thread(() -> {
            try {
                fileAllService.mergeAction();
                log.info("操作完成。");
                Platform.runLater(() -> {
                    TooltipUtil.showToast("操作完成。");
                });
            } catch (Exception e) {
                log.error("操作失败：", e);
            }
        }).start();
    }

    @FXML
    private void saveFilePathAction(ActionEvent event) {
        File file = FileChooserUtil.chooseDirectory();
        if (file != null) {
            saveFilePathTextField.setText(file.getPath());
        }
    }
}
